<?php

class cms_comment_model extends CI_Model {

    protected $_instance = array(
        'table' => 'cms_comment',
        'primaryKey' => 'id',
    );
    public $_data = null;

    protected $_status = array(
        1 => "Đã duyệt",
        2 => "Chưa duyệt"
    );

    protected $_types = array(
        1 => "Bình luận bài viết"
    );

    const COMMENT_STATUS_APPROVED = 1;
    const COMMENT_STATUS_DISAPPROVED = 2;

    public function get_comments() {
        $query = $this->db->get($this->_instance['table']);
        $list_comment = array();
        foreach ($query->result() as $row) {
            $list_comment[] = $row;
        }
        $query->free_result();
        return $list_comment;
    }

    public function get_collection($start, $limit, $conds = null, $orders = null) {
        $query = 'SELECT * FROM ' . $this->_instance['table'];
        if (is_array($conds)) {
            $conds = implode(' AND ', $conds);
        }
        if (!empty($conds)) {
            $query .= ' WHERE ' . $conds;
        }
        if (!empty($orders)) {
            $query .= ' ORDER BY ' . $orders;
        }
        $query .= ' LIMIT ' . $start . ', ' . $limit;
        $query = $this->db->query($query);
        $list_comment = array();
        foreach ($query->result() as $row) {
            $list_comment[] = $row;
        }
        $query->free_result();
        return $list_comment;
    }

    public function add_comment($data) {
        $data = $this->auto_render($data);
        $id = false;
        if (empty($data['error'])) {
            $this->db->insert($this->_instance['table'], $data);
            $id = mysql_insert_id();
        } else {
            throw new Exception($data['error']);
            return false;
        }
        return $id;
    }

    public function update_comment($id, $data) {
        $data = $this->auto_render($data);
        $CI = & get_instance();
        $data['user_id'] = $CI->session->userdata('admin_id');
        if (empty($data['error'])) {
            //Update DB
            $this->db->where('id', $id);
            $this->db->update($this->_instance['table'], $data);
            //Update Memcache
            $CI->st_registry->deleteKey('cms/comment_by_id/' . $id);
            if (!empty($data['alias'])) {
                $CI->st_registry->deleteKey('cms/comment_by_alias/' . $data['alias']);
            }
        } else {
            throw new Exception($data['error']);
            return false;
        }
        return $this;
    }

    public function load_by_id($id, $reload = true) {
        $CI =& get_instance();
        if ($reload) {
            $query = $this->db->get_where($this->_instance['table'], array('id' => $id), 1, 0);
            $this->_data = $query->row();
            $this->_data = $this->auto_render_load($this->_data);
            $CI->st_registry->setKey('cms/comment_by_id/' . $id, $this->_data, CACHE_TIMEOUT);
            $query->free_result();
        } else {
            $this->_data = $CI->st_registry->getKey('cms/comment_by_id/' . $id);
            if (!$this->_data) {
                return $this->load_by_id($id, true);
            }
        }
        return $this->_data;
    }

    public function auto_render_load($data) {
        return $data;
    }

    // Check Exists Product By Field, Value
    public static function exists_comment($field, $value, $reload = false) {
        $CI =& get_instance();
        if ($reload) {
            $query = $CI->db->get_where($CI->_instance['table'], array($field => $value), 1, 0);
            $data = $query->row();
            if (!empty($data) && intval($data->id) > 0) {
                $query->free_result();
                $CI->st_registry->setKey('cms/comment_by_' . $field . '/' . $value, $data, CACHE_TIMEOUT);
                return $data;
            }
        } else {
            $data = $CI->st_registry->getKey('cms/comment_by_' . $field . '/' . $value);
            if (empty($data)) {
                return $CI->exists_comment($field, $value, true);
            }
        }
        return false;
    }

    public function get_data($data, $field) {
        if (is_string($data) && strstr($data, ':')) {
            $data = unserialize($data);
        }
        if (!is_array($data)) {
            return false;
        }
        if (isset($data[$field])) {
            return $data[$field];
        }
        return false;
    }

    public function delete_comment($id) {
        $this->db->delete($this->_instance['table'], array('id' => $id));
        // Delete From Memcache
        $CI =& get_instance();
        $CI->st_registry->deleteKey('cms/comment_by_id/' . $id);
        return $this;
    }

    private function auto_render($data) {
        if (empty($data['ip_address'])) {
            $data['ip_address'] = $_SERVER['REMOTE_ADDR'];
        }
        return $data;
    }

    public static function get_edit_url($id = null) {
        if (empty($id)) {
            return site_url('admin/cms/comment/edit/', true);
        }
        return site_url('admin/cms/comment/edit/id/' . $id, true);
    }

    public function get_list_types() {
        return $this->_types;
    }

    public function get_list_status() {
        return $this->_status;
    }

    public function parse_alias($alias) {
        $alias = trim($alias);
        if (!is_string($alias)) {
            return null;
        }
        $data = array('alias' => $alias);
        switch ($alias) {
            case 'approved':
                $data['status'] = cms_comment_model::COMMENT_STATUS_APPROVED;
                $data['label'] = $this->get_status_label($data['status']);
                break;
            case 'disapproved':
                $data['status'] = cms_comment_model::COMMENT_STATUS_DISAPPROVED;
                $data['label'] = $this->get_status_label($data['status']);
                break;
            default:
                $data['status'] = cms_comment_model::COMMENT_STATUS_DISAPPROVED;
                $data['label'] = $this->get_status_label($data['status']);
                break;
        }
        return $data;
    }

    public function get_status_label($status) {
        $status = intval($status);
        switch ($status) {
            case cms_comment_model::COMMENT_STATUS_APPROVED:
                return 'đã duyệt';
            case cms_comment_model::COMMENT_STATUS_DISAPPROVED:
                return 'hủy duyệt';
            default:
                return 'hủy duyệt';
                break;
        }
    }
}

?>